#include <iostream>
using namespace std;

const int INF = 0x3f3f3f3f;
int a[105][105];
int dis[105];
bool vis[105];
int n, m;
void Prime()
{
    for (int i = 1; i <= n; i++)
    {
        dis[i] = a[1][i];
        vis[i] = false;
    }
    vis[1] = true;
    dis[1] = 0;
    int ans = 0;
    for (int i = 2; i <= n; i++)
    {
        int p = -1;
        int minn = INF;
        for (int j = 1; j <= n; j++)
        {
            if (!vis[j] && dis[j] < minn)
                minn = dis[p = j];
        }
        ans += minn;
        vis[p] = true;
        for (int j = 1; j <= n; j++)
        {
            if (!vis[j] && dis[j] > a[p][j])
                dis[j] = a[p][j];
        }
    }
    cout << ans << endl;
}
int main()
{
    while (cin >> n, n)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
                if (i == j)
                    a[i][j] = 0;
                else
                    a[i][j] = INF;
        }
        m = n * (n - 1) / 2;
        int x, y, z, f;
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d%d%d", &x, &y, &z, &f);
            if (f == 1)
                a[x][y] = a[y][x] = 0;
            else
                a[x][y] = a[y][x] = z;
        }
        Prime();
    }
    return 0;
}
// https: //blog.csdn.net/hurmishine/article/details/52133090